package com.chixing.service.impl;

import com.chixing.mapper.*;
import com.chixing.pojo.*;
import com.chixing.service.ReviewService;
import com.chixing.util.ServerResponse;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Service
public class ReviewServiceImpl implements ReviewService {

    @Autowired
    private ReviewMapper reviewMapper;

    @Autowired
    private ReplyMapper replyMapper;

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private PraiseMapper praiseMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ServerResponse getReviewByCarId(Integer carId,Integer pageNum) {
        //  根据车辆id  分页查询评论 用户信息 和 评论信息 和 回复者信息及回复的内容

        Sort sort = Sort.by(Sort.Direction.DESC, "DEVID").and(Sort.by(Sort.Direction.DESC,"review_time"));//多条件DEVID、time
        Query query = new Query(Criteria.where("carId").is(carId));
        System.out.println(query);
        List<Review> reviewList = mongoTemplate.find(query.with(sort).limit(1000),Review.class);
        Page<Object> page = PageHelper.startPage(pageNum,5);
        PageInfo pageInfo = page.toPageInfo();

        List<ReviewCustomerReplyVO> reviewCustomerReplyVOList = new ArrayList<>();
        List<ReplyCustomerVO> replyCustomerVOList;
        for(Review review : reviewList){
            Query query1=new Query(Criteria.where("reviewId").is(review.getReviewId()).and("status").is(0));
            List<Reply> replyList=mongoTemplate.find(query1,Reply.class);
            System.out.println("回复为:"+replyList);
//            ReplyExample replyExample = new ReplyExample();
//            replyExample.createCriteria().andReviewIdEqualTo(review.getReviewId()).andStatusEqualTo(0);
//            List<Reply> replyList = replyMapper.selectByExample(replyExample);      //通过评价id查询所有回复评价的评论
            replyCustomerVOList = new ArrayList<>();          // 用来接收所有回复评价的评论
            Customer customer = customerMapper.selectByPrimaryKey(review.getCustId()); // 查询评价用户
            if(replyList.size()!=0) {
                for (Reply reply : replyList) {
                    System.out.println(reply);
                    Customer replyCustomer = customerMapper.selectByPrimaryKey(reply.getCustId()); //查询所有的回复用户
                    replyCustomerVOList.add(new ReplyCustomerVO(replyCustomer, reply));  // 将所有的回复用户和回复一个VO集合中
                }
            }
            reviewCustomerReplyVOList.add(new ReviewCustomerReplyVO(review,customer, replyCustomerVOList)); // 将当前评价用户及评价信息，以及回复者和回复信息封
        }
        pageInfo.setList(reviewCustomerReplyVOList);
        if(reviewCustomerReplyVOList.size() != 0)
            return ServerResponse.getSucccess(pageInfo);
        return ServerResponse.getFail(null);
    }

    //点赞操作
    @Override
    public ServerResponse updateReviewByCarId(Integer reviewId, Integer custId, Integer status) {
        int row = 0;
        int rows = 0;
        Review review = reviewMapper.selectByPrimaryKey(reviewId);
        PraiseExample praiseExample = new PraiseExample();
        praiseExample.createCriteria().andReviewIdEqualTo(reviewId).andCustIdEqualTo(custId);

        List<Praise> praiseList = praiseMapper.selectByExample(praiseExample);
        Praise praise;
        boolean existDataInDb;
        if(praiseList.size()>0) {
            praise = praiseList.get(0);
            praise.setPraiseTime(new Date());
            existDataInDb = true;
        } else {
            praise = new Praise();
            praise.setCustId(custId);
            praise.setReviewId(review.getReviewId());
            praise.setPraiseTime(new Date());
            existDataInDb = false;
        }

        if(status == 0) { // 0为有效
            review.setReviewPraised(review.getReviewPraised() + 1);
            praise.setStatus(0);
            row =reviewMapper.updateByPrimaryKey(review);
            if(existDataInDb)
                rows = praiseMapper.updateByPrimaryKey(praise);
            else
                rows = praiseMapper.insert(praise);
        }else if(status==1){
            praise.setStatus(1);
            rows = praiseMapper.updateByPrimaryKey(praise);
            review.setReviewPraised(review.getReviewPraised() - 1);
            row =reviewMapper.updateByPrimaryKey(review);

        }
        if(rows>0 && row>0){
            return ServerResponse.updateSucccess(review.getReviewPraised());
        }
        return ServerResponse.updateFail(null);
    }
}